package com.hxzy.shiro;

import com.hxzy.common.utils.BCyrptUtil;
import com.hxzy.entity.AdminUser;
import com.hxzy.service.AdminUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * shiro中Realm实现类说明
 *
 * @author admin
 * @date 2021-09-06
 */
@Component
public class AdminUserRealm  extends AuthorizingRealm {

    /**
     * 用户管理业务逻辑
     */
    private AdminUserService  adminUserService;

    @Autowired
    public void setAdminUserService(AdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    /**
     * 1、如何实现登录的 (查询 sys_admin_user)
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        //1、取得Controller传过来的对象
        UsernamePasswordToken   usernamePasswordToken= (UsernamePasswordToken) authenticationToken;
        //2、取得用户名
        String userName=usernamePasswordToken.getUsername();
        //3、取得明文密码
        String pwd=new String(usernamePasswordToken.getPassword());

        //3、调用登录方法
        AdminUser dbUser = this.adminUserService.findByLoginName(userName);
        // 用户名不存
        if(dbUser==null){
            throw new UnknownAccountException();
        }

        //传明文，数据库密码
        boolean isCorrect= BCyrptUtil.checkpw(pwd,  dbUser.getLoginPassword());
        if(!isCorrect){
           throw new IncorrectCredentialsException();
        }

        //判断账户是否被锁定
        if(dbUser.getLocked()==1){
            throw new LockedAccountException();
        }

        //登录成功，返回成功用户信息
        SimpleAuthenticationInfo  simpleAuthenticationInfo=new SimpleAuthenticationInfo(dbUser,pwd,super.getName());

        return simpleAuthenticationInfo;
    }

    /**
     * 2、用户如何来授权的 (sys_role, sys_menu,sys_role_menu, sys_admin_user_role)
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        AdminUser adminUser= (AdminUser) principalCollection.getPrimaryPrincipal();

        //查询当前用户的角色权限有哪些放到Set<String>
        Set<String> ownRole = this.adminUserService.findOwnRole(adminUser.getAdminUserId());

        //查询当前用户的角色的菜单权限有哪些，把它放到Set<String>
        Set<String> ownRoleOwnMenu = this.adminUserService.findOwnRoleOwnMenu(adminUser.getAdminUserId());

        SimpleAuthorizationInfo  simpleAuthorizationInfo=new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setRoles(ownRole);
        simpleAuthorizationInfo.setStringPermissions(ownRoleOwnMenu);

        return simpleAuthorizationInfo;
    }


}
